﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace SerialPort_Assistant
{
    partial class Q_Learning
    {
        public void DisplayQTable()
        {
            for (int i = 0; i < env.Observation_Space_N; i++)
            {
                for (int j = 0; j < env.Action_Space_N; j++)
                {
                    Console.Write(q_Table[i, j] + " ");
                }
                Console.WriteLine();
            }
        }

        public void Save(string filename)
        {
            // Convert q_Table to a string representation
            string qTableString = ConvertToString(q_Table);

            // Write the string to a file
            File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), filename), qTableString);
        }

        public void Load(string filePath)
        {
            // Read the contents of the file as a string
            string qTableString = File.ReadAllText(filePath);

            // Convert the string to a q_Table
            q_Table = ConvertFromString(qTableString);
        }


        private string ConvertToString(double[,] q_Table)
        {
            // Create a string builder to store the string representation of the q_Table
            StringBuilder sb = new StringBuilder();

            // Create a new instance of NumberFormatInfo
            NumberFormatInfo nfi = new NumberFormatInfo();
            nfi.NumberDecimalSeparator = ".";

            // Iterate through the rows and columns of the q_Table
            for (int row = 0; row < q_Table.GetLength(0); row++)
            {
                for (int col = 0; col < q_Table.GetLength(1); col++)
                {
                    // Append the value at the current cell to the string builder using the modified NumberFormatInfo object
                    sb.Append(q_Table[row, col].ToString("F", nfi));

                    // Add a comma after each value, except for the last value in the row
                    if (col < q_Table.GetLength(1) - 1)
                        sb.Append(',');
                }
                // Add a newline after each row, except for the last row
                if (row < q_Table.GetLength(0) - 1)
                    sb.AppendLine();
            }

            // Return the string representation of the q_Table
            return sb.ToString();
        }

        private double[,] ConvertFromString(string qTableString)
        {
            // Create a new instance of NumberFormatInfo
            NumberFormatInfo nfi = new NumberFormatInfo();
            nfi.NumberDecimalSeparator = ".";

            // Split the string into rows
            string[] rows = qTableString.Split('\n');

            // Create a 2D array to store the values of the q_Table
            double[,] qTable = new double[rows.Length, rows[0].Split(',').Length];

            // Iterate through the rows and columns of the q_Table
            for (int row = 0; row < qTable.GetLength(0); row++)
            {
                // Split the current row into columns
                string[] cols = rows[row].Split(',');

                for (int col = 0; col < qTable.GetLength(1); col++)
                    // Convert the string value at the current cell to a double and store it in the q_Table
                    qTable[row, col] = double.Parse(cols[col], NumberStyles.Float, nfi);
            }

            // Return the q_Table
            return qTable;
        }
    }
}
